#!/bin/bash

#platform_name=$1
#board=$2
#kernel_version=$3
current_path=`pwd`


#cd $current_path/../../../kernel_files/$platform_name
#IMAGE_FILE_PATH=`ls | grep ${board} | grep $kernel_version`

#echo -e "kernel update file: $IMAGE_FILE_PATH\n"
#echo -e "\e[33mAre you sure to update image?(y/n)\e[0m"
#while [ 1 ];do
#	if [ -z $com ];then
#		read com
#	fi
#	break
#done
#
#if [ "$com" == "y"  ];then
#	image=`ls Image*`
#
#	echo -e "\n\e[32mupdating image and dtb  ...\e[0m"
#	sudo cp $image /boot/Image
#	sudo cp tegra194-p3668-all-p3509-0000.dtb /boot/.
#	sync
#	echo -e "\e[32mupdate image and dtb  done\e[0m\n"
#	
#	reboot_flag=1
#else
#	echo -e "\n\e[32mImage update canceled!\e[0m\n"
#fi


reboot_flag=0
echo -e "\e[33mAre you sure to change file system?(y/n)\e[0m"
while [ 1 ];do
	if [ -z ${ch_sys} ];then
		read ch_sys
	fi
	break
done

if [ "${ch_sys}" == "y" ];then
	num=`find /dev -name "sd*[0-9]" -o -name "nvme0n1p[0-9]"| wc -l`
	echo -e "\nchoose device :"
	for ((i = 1; i <= ${num}; i++))
	do
	        dev_name[$i]=`find /dev -name "sd*[0-9]" -o -name "nvme0n1p[0-9]" | head -n $i | tail -n 1`
	        dev_size[$i]=`sudo fdisk -l | grep ${dev_name[$i]} | awk '{print $5}'`
	        echo -e "$i. ${dev_name[$i]}    ${dev_size[$i]}"
	done
	
	while [ 1 ];
	do
	        #input
	        echo -e "\nInput device num"
	
	        read opt
	        #echo ${opt}
	
	        if [ -z "${opt}" ];then
	                echo -e "Please input!\n"
       	        	continue
       		fi

        	#judge number
        	if [ ! -z "`echo ${opt} | grep [^0-9]`" ];then
                	echo -e "Please Enter the Right Number!\n"
                	sleep 0.5
                	continue
        	fi

        	#judge range
        	if [ ${opt} -gt ${num} ];then
        	        echo -e "Number too large!\n"
	                sleep 0.5
        	        continue
        	fi

		#judge device
		echo -e "\nEnsure the device : ${dev_name[${opt}]} (y/n)"
		while [ 1 ];do
			if [ -z $com ];then
				read com
			fi
			break
		done
		if [ "${com}" == "y" ];then
			break
		fi
		unset com

	done



	#sudo cp tztek_initrd /boot/.
	sudo cp extlinux.conf /boot/extlinux/.
	sync
	file=/boot/extlinux/extlinux.conf

	UUID=`sudo blkid | grep ${dev_name[${opt}]} | awk -F'"' '{print $2}'`
	echo "UUID : ${UUID}"
	if [ ${#UUID} != 0 ];then
		PARTUUID="PARTUUID=${UUID}"
	else
		PARTUUID="/dev/mmcblk0p1"
	fi
	c_line=`cat -n ${file} | grep APPEND | head -n 1 | awk '{print $1}'`
	echo "change line : ${c_line}"
	data="\ \ \ \ \ \ APPEND \${cbootargs} root=${PARTUUID} rw rootwait rootfstype=ext4 console=ttyTCU0,115200n8 console=tty0 net.ifnames=0"

	sudo sed -i "${c_line}c ${data}" ${file}
	sync
	echo -e "\e[32mupdate extlinux.conf done\e[0m\n"

	cd `df | grep ${dev_name[${opt}]} | awk '{print $6}'`/var/lib/gdm3
	ls -a | grep -v greeter-dconf-defaults | xargs sudo chown gdm:gdm -R
	sync
	reboot_flag=1
fi

if [ "${reboot_flag}" == "1" ];then
	echo -e "\e[32mReady to reboot!\e[0m\n"
	sleep 2
	reboot
fi

unset ch_sys
unset com
